using System;

namespace Measurements
{
    public abstract class Measurement<TUnit> : IComparable, IComparable<Measurement<TUnit>>, IEquatable<Measurement<TUnit>>
        where TUnit : MeasurementUnit
    {
        protected double BaseUnitValue { get; set; }
        
        public double this[TUnit unit]
        {
            get 
            { 
                if (unit == null) throw new ArgumentNullException("unit");
                if (unit.FromBaseUnit == null) throw new ArgumentException("Delegate is null.");
                
                return unit.FromBaseUnit(BaseUnitValue); 
            }
            set 
            {
                if (unit == null) throw new ArgumentNullException("unit");
                if (unit.ToBaseUnit == null) throw new ArgumentException("Delegate is null.");
                
                BaseUnitValue = unit.ToBaseUnit(value);
            }
        }
        
        public int CompareTo(Measurement<TUnit> other)
        {
            return BaseUnitValue.CompareTo(other.BaseUnitValue);
        }

        public int CompareTo(object obj)
        {
            // TODO: Should the null check come after the type check?

            if (obj == null) return 1;
            if (obj.GetType() != GetType()) throw new ArgumentException();

            return CompareTo((Measurement<TUnit>)obj);
        }
        
        public bool Equals(Measurement<TUnit> other)
        {
            return BaseUnitValue == other.BaseUnitValue;
        }
        
        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            if (obj.GetType() != GetType()) return false;

            return Equals((Measurement<TUnit>)obj);
        }
        
        public bool Equals(Measurement<TUnit> other, double tolerance)
        {
            return Math.Abs(BaseUnitValue - other.BaseUnitValue) <= tolerance;
        }        
        
        public bool Equals(Measurement<TUnit> other, double tolerance, TUnit unit)
        {
            return Math.Abs(this[unit] - other[unit]) <= tolerance;
        }        
        
        public override int GetHashCode()
        {
            return BaseUnitValue.GetHashCode();
        }
        
        public static bool operator ==(Measurement<TUnit> t1, Measurement<TUnit> t2)
        {
            return t1.BaseUnitValue == t2.BaseUnitValue;
        }

        public static bool operator !=(Measurement<TUnit> t1, Measurement<TUnit> t2)
        {
            return t1.BaseUnitValue != t2.BaseUnitValue;
        }

        public static bool operator >(Measurement<TUnit> t1, Measurement<TUnit> t2)
        {
            return t1.BaseUnitValue > t2.BaseUnitValue;
        }

        public static bool operator <(Measurement<TUnit> t1, Measurement<TUnit> t2)
        {
            return t1.BaseUnitValue < t2.BaseUnitValue;
        }

        public static bool operator >=(Measurement<TUnit> t1, Measurement<TUnit> t2)
        {
            return t1.BaseUnitValue >= t2.BaseUnitValue;
        }

        public static bool operator <=(Measurement<TUnit> t1, Measurement<TUnit> t2)
        {
            return t1.BaseUnitValue <= t2.BaseUnitValue;
        }        
        
        public static MeasurementUnitConvertible<TUnit> Convert(double measurement)
        {
            return new MeasurementUnitConvertible<TUnit>(measurement);   
        }

        public string ToString(string format, TUnit unit)
        {
            if (unit == null) throw new ArgumentNullException("unit");
            return BaseUnitValue.ToString(format) + " " + unit.Unit;
        }
    }
    
    public abstract class Measurement<TMeasurement, TUnit> : Measurement<TUnit>, ICloneable
        where TUnit : MeasurementUnit
        where TMeasurement : Measurement<TMeasurement, TUnit>, new()
    {
        public static TMeasurement Create(double measurement)
        {
            var obj = new TMeasurement();
            obj.BaseUnitValue = measurement;
            return obj;
        }
        
        public static TMeasurement Create(double measurement, TUnit unit)
        {
            var obj = new TMeasurement();
            obj[unit] = measurement;
            return obj;
        }      

        public object Clone()
        {
            var obj = new TMeasurement();
            obj.BaseUnitValue = BaseUnitValue;
            return obj;
        }
    }
}
